import request from './request'

// 分页查询成绩记录
export const getScoreList = (params) => {
  return request.get('/scores', { params })
}

// 录入单个成绩
export const recordScore = (data) => {
  return request.post('/scores', data)
}

// 批量录入成绩
export const batchRecordScores = (data) => {
  return request.post('/scores/batch', data)
}

// 更新成绩
export const updateScore = (id, data) => {
  return request.put(`/scores/${id}`, data)
}

// 删除成绩
export const deleteScore = (id) => {
  return request.delete(`/scores/${id}`)
}

// 根据项目查询成绩列表
export const getEventScores = (eventId, params) => {
  return request.get(`/scores/event/${eventId}`, { params })
}

// 根据运动员查询成绩列表
export const getAthleteScores = (athleteId) => {
  return request.get(`/scores/athlete/${athleteId}`)
}

// 获取项目前N名成绩
export const getTopScores = (eventId, params) => {
  return request.get(`/scores/event/${eventId}/top`, { params })
}

// 重新计算项目排名
export const calculateRankings = (eventId, params) => {
  return request.post(`/scores/event/${eventId}/rankings`, null, { params })
}

// 根据ID查询成绩详情
export const getScoreDetail = (id) => {
  return request.get(`/scores/${id}`)
}

// 获取成绩统计信息
export const getScoreStatistics = (params) => {
  return request.get('/scores/statistics', { params })
}

// 根据项目ID获取该项目的报名列表（用于成绩录入）
export const getEventRegistrations = (eventId) => {
  return request.get(`/registration/event/${eventId}/approved`)
}

// 获取比赛项目列表
export const getEventList = (params) => {
  return request.get('/events', { params })
}

// 导出成绩数据
export const exportScoreData = (params) => {
  return request.get('/scores/export', { 
    params,
    responseType: 'blob'
  }).then(response => {
    // 创建下载链接
    const blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
    const url = window.URL.createObjectURL(blob)
    const a = document.createElement('a')
    a.href = url
    a.download = `成绩查询结果_${new Date().toISOString().slice(0, 10)}.xlsx`
    document.body.appendChild(a)
    a.click()
    document.body.removeChild(a)
    window.URL.revokeObjectURL(url)
  })
}
